from flask.views import View
from flask import request
from alembic.models import Host, DBSession
import json


class BaseView(View):
    def not_specify(self):
        return "Not Specify"

    def get(self):
        return self.not_specify()

    def post(self):
        return self.not_specify()

    def dispatch_request(self):
        if request.method == "GET":
            return self.get()
        if request.method == "POST":
            return self.post()
        return self.not_specify()


class ImageView(BaseView):
    def get(self):
        return "hhhh"


class HostView(BaseView):
    def get(self):
        """ list host"""
        session = DBSession()
        hosts = session.query(Host).all()
        return json.dumps(hosts)

    def post(self):
        """ add host"""
        import pdb;pdb.set_trace()
        return "asdfsadf"

    def put(self):
        return "asdfsaf"

def register(app):
    app.add_url_rule("/images/", view_func=ImageView.as_view("images"))
    app.add_url_rule("/hosts/", view_func=HostView.as_view("hosts"),
        methods=["GET", "POST"])
    return app
